package com.example.dataservice.serviceimpl;

import com.example.common.util.CommonUtil;
import com.example.dataservice.mapper.FinanceAccountMapper;
import com.example.dataservice.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.example.api.model.FinanceAccount;
import org.example.api.model.User;
import org.example.api.pojo.UserCenterInfo;
import org.example.api.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @description: TODO
 * @author: uu
 * @data: 2023/7/14 下午 09:18
 * @version: v1.0
 */
@DubboService(interfaceClass = UserService.class, version = "1.0")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private FinanceAccountMapper financeAccountMapper;

    @Value("${ylb.config.salt}")
    private String salt;

    @Override
    /*用户实名认证成功，根据用户信息*/
    public Integer modifyUserInfo(String phone, String name, String idCard) {
        Integer count = 0;
        if (!StringUtils.isAnyBlank(phone, name, idCard)){
            count = userMapper.updateUserInfo(phone, name, idCard);
            System.out.println(count);
        }
        return count;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    /*用户登录*/
    public User userLogin(String phone, String password) {
        User user = null;
        if (CommonUtil.checkPhone(phone) && password != null && password.length() == 32){
            String newPassword = DigestUtils.md5DigestAsHex((password + salt).getBytes());
            user = userMapper.selectUserLogin(phone, newPassword);
            // 更新用户最后登录时间
            if (user !=null){
                user.setLastLoginTime(new Date());
                userMapper.updateByPrimaryKeySelective(user);
            }
        }
        return user;
    }


    @Override
    /*验证手机号是否存在*/
    public User queryByPhone(String phone) {
        User user = null;
        if (CommonUtil.checkPhone(phone)){
            user = userMapper.selectByPhone(phone);
        }
        return user;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    /* 用户注册 */
    public synchronized int userRegister(String phone, String password) {
        int result = 0;
        if (CommonUtil.checkPhone(phone) && password!=null && password.length() == 32){
            // 判断手机号是否存在
            if (userMapper.selectByPhone(phone) == null){
                User user = new User();
                user.setPhone(phone);
                // 注册密码的md5 二次加密，给原始的密码加盐
                String newPassword = DigestUtils.md5DigestAsHex((password + salt).getBytes());
//                System.out.println(newPassword);
                user.setLoginPassword(newPassword);
                // 插入用户信息，返回自增字段id
                user.setAddTime(new Date());
                int count = userMapper.insertRegisterUser(user);
                if (count == 1){
                    FinanceAccount account = new FinanceAccount();
                    account.setUid(user.getId());
                    account.setAvailableMoney(new BigDecimal("0"));
                    // 给注册用户添加资金账户
                    count  += financeAccountMapper.insertSelective(account);
                    if (count == 2){
                        // 注册成功
                        result = 1;
                    }
                }
            }else {
                // 手机号已被注册
                result = 2;
            }
        }

        return result;
    }

    /*用户中心*/
    @Override
    public UserCenterInfo queryUserCenter(Integer id) {
        UserCenterInfo userCenterInfo = null;
        if (id != null){
            userCenterInfo = userMapper.selectUserCenterInfo(id);
//            System.out.println(userCenterInfo);
        }
        return userCenterInfo;
    }

    @Override
    /*根据id查询用户信息*/
    public Boolean queryUserInfo(Integer id) {
        if (id != null && id > 0){
            User user = userMapper.selectByPrimaryKey(id);
            if (user.getName() != null && user.getIdCard()!=null){
                if (user.getIdCard().length() == 18 || user.getIdCard().length() == 15){
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    /*通过主键查询用户信息*/
    public User queryById(Integer id) {
        User user = null;
        if (id != null && id > 0){
            user = userMapper.selectByPrimaryKey(id);
        }
        return user;
    }

}
